package com.lemner.motion.activity;

import android.app.Instrumentation;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.lemner.motion.R;
import com.lemner.motion.base.BaseActivity;
import com.lemner.motion.util.DensityUtil;
import com.lemner.motion.util.FullScreen;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RunDetailActivity extends BaseActivity {

    private AMap aMap = null;
    private MapView run_map;
    private PolylineOptions polylineOptions;
    private double top,bottom,left,right;
    private List<Polyline> polylineList = new ArrayList<Polyline>();
    private Button btn_back;
    private List<Integer> colors=new ArrayList<Integer>();
    private List<Integer> colorList=new ArrayList<Integer>();
    private List<LatLng> latLngs=new ArrayList<LatLng>();
    private List<LatLng> latLngList=new ArrayList<LatLng>();
    private AMapLocationClient mlocationClient = null;


    @Override
    public int getLayoutFile() {
        return R.layout.activity_run_detail;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        run_map.onCreate(savedInstanceState);
        initMap();
    }
    @Override
    public void initView() {
        FullScreen.fullScreen(RunDetailActivity.this,0x33131313);
        run_map=(MapView)findViewById(R.id.run_map);
        btn_back=(Button)findViewById(R.id.btn_back);
    }

    @Override
    public void initEvent() {
     btn_back.setOnClickListener(this);
    }

    @Override
    public void initData() {
        //把服务器颜色数组转换
        colors=(List<Integer>)getIntent().getSerializableExtra("colors");
        for(int j=0;j<colors.size();j++){
            String color= String.valueOf(colors.get(j));
            String str2=color.replace(" ", "");//去掉所用空格
            List<String> list= Arrays.asList(str2.split(","));
//            String red=Integer.toHexString());
            String red=String.format("%02x",Integer.parseInt(list.get(1)));
            String green=String.format("%02x",Integer.parseInt(list.get(0)));
            String blue=String.format("%02x",Integer.parseInt(list.get(2)));
            Integer num= Integer.parseInt("-"+red+green+blue,16)-256;
            if(num==-16777216)      //黄
                colorList.add(num-256);
            else
                colorList.add(num);
        }
        //把服务器字符数组转成LatLng数组
        latLngs=(List<LatLng>) getIntent().getSerializableExtra("latLngs");
        for(int i=0;i<latLngs.size();i++){
            String latlngs= String.valueOf(latLngs.get(i));
            String str2=latlngs.replace(" ", "");//去掉所用空格
            List<String> list= Arrays.asList(str2.split(","));
            double latitude= Double.parseDouble(list.get(0));
            double longitude= Double.parseDouble(list.get(1));
            latLngList.add(new LatLng(latitude,longitude));
        }


    }

    public void initMap() {
        if (aMap == null) {
            aMap = run_map.getMap();
        }
        if(latLngs.size()!=0) {
            getZoom();
            reDrawMap();
        }
    }
    public void getZoom(){
        top=bottom=latLngList.get(0).latitude;
        left=right=latLngList.get(0).longitude;
        for(int i=0;i<latLngList.size();i++){
            top=latLngList.get(i).latitude>top?latLngList.get(i).latitude:top;
            left=latLngList.get(i).longitude<left?latLngList.get(i).longitude:left;
            bottom=latLngList.get(i).latitude<bottom?latLngList.get(i).latitude:bottom;
            right=latLngList.get(i).longitude>right?latLngList.get(i).longitude:right;
        }
        //移到地图中心点
        aMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(new LatLng((top + bottom) / 2, (left + right) / 2), 18, 0, 0)));//地图显示到中间
        double distanceH= AMapUtils.calculateLineDistance(new LatLng(left,0),new LatLng(right,0));//水平距离
        double distanceV=AMapUtils.calculateLineDistance(new LatLng(0,top),new LatLng(0,bottom));//垂直距离
        double distanceMax=distanceH>distanceV?distanceH:distanceV;
        if(distanceMax<80)
            aMap.moveCamera(CameraUpdateFactory.zoomTo(19));//缩放
        else if(distanceMax<175)
            aMap.moveCamera(CameraUpdateFactory.zoomTo(18));//缩放
        else if(distanceMax<350)
            aMap.moveCamera(CameraUpdateFactory.zoomTo(17));//缩放
        else if(distanceMax<700)
            aMap.moveCamera(CameraUpdateFactory.zoomTo(16));//缩放
        else if(distanceMax<1200)
            aMap.moveCamera(CameraUpdateFactory.zoomTo(15));//缩放
        else if(distanceMax<3000)
            aMap.moveCamera(CameraUpdateFactory.zoomTo(14));//缩放
        else if(distanceMax<6000)
            aMap.moveCamera(CameraUpdateFactory.zoomTo(13));//缩放
        else if(distanceMax<8000)
            aMap.moveCamera(CameraUpdateFactory.zoomTo(12));//缩放
        else aMap.moveCamera(CameraUpdateFactory.zoomTo(5));//缩放
    }
    public void reDrawMap(){
        polylineOptions = new PolylineOptions().addAll(latLngList).width(20f).useGradient(true);
        polylineOptions.colorValues(colorList);
        aMap.addPolyline(polylineOptions);
        final Marker marker1 = aMap.addMarker(new MarkerOptions().position(latLngList.get(0)).icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.pathstartpointimage3x))));
        final Marker marker2 = aMap.addMarker(new MarkerOptions().position(latLngList.get(latLngList.size()-1)).icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.pathendpointimage3x))));
    }

    @Override
    public void onClick(View v) {
           switch (v.getId()){
               case R.id.btn_back:
                   onBack();
//                   Intent intent=new Intent(RunDetailActivity.this,StartRunActivity.class);
//                   intent.putExtra("list",true);
//                   startActivity(intent);
//                   finish();
           }
    }

    public void onBack(){
        new Thread(){
            public void run() {
                try{
                    Instrumentation inst = new Instrumentation();
                    inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                }
                catch (Exception e) {
                    Log.e("Exception when onBack", e.toString());
                }
            }
        }.start();
    }


}
